//
//  PersonalDataViewController.m
//  SeaMelonSeeds_Counselor
//
//  Created by 尤振宇 on 2017/7/18.
//  Copyright © 2017年 杨笑. All rights reserved.
//

#import "PersonalDataViewController.h"
#import "PersonalDataView.h"
#import "DBAccess.h"

@interface PersonalDataViewController ()
@property (weak, nonatomic) IBOutlet UIView *backView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *back_H;
@property (weak, nonatomic) IBOutlet UIImageView *headPortrait;
@property(nonatomic,weak)UILabel *describe;
@property(nonatomic,strong)NSMutableArray *views;
@end

@implementation PersonalDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self.headPortrait roundedCorners:85/2];
    [self initView];
    self.view.backgroundColor = [UIColor whiteColor];
}

-(void)loadData
{
    WeakSelf(self)
    UserDataApi *api = [[UserDataApi alloc]initRequestProfileWithUserID:self.userData.id];
    [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        if (request.responseJSONObject) {
            NSDictionary *dic = [request.responseJSONObject objectForKey:@"data"];
            [weakself.userData setValuesForKeysWithDictionary:dic];
        }
        
        [weakself.headPortrait sd_setImageWithURL:[NSURL URLWithString:weakself.userData.avatar]];
        for (int i = 0; i < weakself.views.count; i++) {
            PersonalDataView *showView = weakself.views[i];
            if (i == 0) {
                showView.subTitle.text = weakself.userData.name;
            }else if (i == 1){
                showView.subTitle.text = weakself.userData.sex?@"女":@"男";
            }else if (i == 2){
                showView.subTitle.text = [weakself.userData.birthdate componentsSeparatedByString:@" "][0];
            }else if (i == 3){
                showView.subTitle.text = weakself.userData.education;
            }else if (i == 4){
                NSString *pro = [[DBAccess sharedInstance] getRegionNameById:[NSString stringWithFormat:@"%d",weakself.userData.province_id]];
                NSString *city = [[DBAccess sharedInstance] getRegionNameById:[NSString stringWithFormat:@"%d",weakself.userData.city_id]];
                showView.subTitle.text = [NSString stringWithFormat:@"%@ %@",pro,city];
            }
        }
        
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        
    }];
}

-(void)initView
{
    CGFloat view_H = 40;
    
    for (int i =0; i<self.views.count; i++) {
        UIView *view = [self.views objectAtIndex:i];
        [self.backView addSubview:view];
        
        UIView *onview;
        if (i > 0) {
            onview = [self.views objectAtIndex:i-1];
        }
        
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(onview?onview.mas_bottom:self.headPortrait.mas_bottom);
            make.left.equalTo(self.backView);
            make.width.equalTo(self.backView);
            make.height.mas_equalTo(view_H);
        }];
    }
    
//    NSString *str = @"啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦lol了啦啦啦啦啦啦啦啦啦啦lol了啦啦啦啦啦啦啦啦啦啦lol了啦啦啦啦啦啦啦啦啦啦lol了啦啦啦啦啦啦啦啦啦啦lol了啦啦啦啦啦啦啦啦啦啦lol了啦啦啦啦啦啦啦啦啦啦lol了啦啦啦啦啦啦啦啦啦啦lol了啦啦啦啦啦啦啦啦啦啦lol了啦啦啦啦啦啦啦啦啦啦lol了";
    
//    UILabel *label = [[UILabel alloc]init];
//    label.numberOfLines = 0;
//    [self.backView addSubview:label];
//    PersonalDataView *onView = [self.views lastObject];
//    [label mas_makeConstraints:^(MASConstraintMaker *make) {
//        CGFloat w = SCREEN_WIDTH - 25 - 25;
////        CGFloat h = [str heightForFontSize:13 andWidth:w] + 18 + 4;
//        
//        make.top.equalTo(onView.mas_bottom).with.offset(12);
//        make.left.equalTo(onView.title.mas_left);
//        make.width.mas_equalTo(w);
////        make.height.mas_equalTo(h);
//        
//        label.width = w;
////        label.height = h;
//    }];
//    self.describe = label;
//    [self complainOf:@""];
    
//    UIView *line = [[UIView alloc]init];
//    line.backgroundColor = kCCCCCC;
//    [self.backView addSubview:line];
//    [line mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(label.mas_bottom).with.offset(12);
//        make.trailing.equalTo(onView);
//        make.width.equalTo(onView);
//        make.height.mas_equalTo(0.5);
//    }];
    
//    self.back_H.constant = 93/*self.back_H.constant 初始高度*/ + view_H * self.views.count + label.height + 12 + 50;
    self.back_H.constant = 93/*self.back_H.constant 初始高度*/ + view_H * self.views.count + 12 + 50;
}

//-(void)complainOf:(NSString *)content
//{
//    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:@"主诉\n  " attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];
//    [str appendAttributedString:[[NSAttributedString alloc]initWithString:content attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13],NSForegroundColorAttributeName:k666666}]];
//    CGFloat h = [content heightForFontSize:13 andWidth:self.describe.width] + 18 + 4;
//    self.describe.height = h;
//    self.describe.attributedText = str;
//    
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSMutableArray *)views
{
    if (!_views) {
        _views = [NSMutableArray array];
        
        NSArray *arr = @[@"姓名",@"性别",@"出生日期",@"学历",@"所在地"];
        for (int i = 0; i < arr.count; i++) {
            NSString *str = arr[i];
            
            PersonalDataView *view = [PersonalDataView ViewWithXib];
            view.title.text = str;
            view.subTitle.text = @"";
            [_views addObject:view];
        }
    }
    return _views;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
